var imageCodeId = "";

function generateUUID() {
    var d = new Date().getTime();
    if (window.performance && typeof window.performance.now === "function") {
        d += performance.now(); //use high-precision timer if available
    }
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}

// 生成一个图片验证码的编号，并设置页面中图片验证码img标签的src属性
function generateImageCode() {
    // 1. 生成一个编号
    // 严格一点的使用uuid保证编号唯一， 不是很严谨的情况下，也可以使用时间戳
    imageCodeId = generateUUID();

    // 2. 拼接验证码地址
    var imageCodeUrl = "image_code?code_id=" + imageCodeId;
    // 3. 设置页面中图片验证码img标签的src属性
    $(".image_captcha").attr("src", imageCodeUrl);
}

generateImageCode();
$(function () {
    // 注册
    $("#sign_up").click(function () {
        var user_name = $("#signin_form[name='user_name']").val();
        var image_code = $("#signin_form[name='image_code']").val();
        var telephone = $("#signin_form[name='telephone']").val();
        var password = $("#signin_form[name='password']").val();
        if (!user_name) {
            alert("请输入用户名");
            return;
        }
        if (!image_code) {
            alert("请输入图片验证码");
            return;
        }
        if (!telephone) {
            alert("请输入电话");
            return;
        }
        if (!password) {
            alert("请输入密码");
            return;
        } else if (password.length < 6) {
            alert("密码长度必须大于6位");
            return;
        }
        var params = {
            "user_name": user_name,
            "image_code": image_code,
            "telephone": telephone,
            "password": password
        };
        $.ajax({
            url: '/signup.html',
            type: 'post',
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.errno == '0') {
                    alert('注册成功');
                    window.location.href = "/index.html"
                } else if (resp.errno == '1001') {
                    alert("验证码错误");
                    generateImageCode()
                } else {
                    alert("注册失败");
                    generateImageCode()
                }

            }
        })
    });


    // 登录
    $("#btn_login").click(function () {
        var telephone = $("#signin_form[name='telephone']").val();
        var password = $("#signin_form[name='password']").val();

        if (!telephone) {
            alert('请输入手机号');
            return
        }
        if (!password) {
            alert('请输入密码');
            return;
        }
        var params = {
            "telephone": telephone,
            "password": password
        };
        $.ajax({
            url: '/signin.html',
            type: 'post',
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.errno == '0') {
                    // alert('登录成功');
                    window.location.href='/index.html'
                } else {
                    alert(resp.errmsg);
                    window.location.reload()
                }
            }
        })

    })
});